<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/3
 * Time: 11:39
 */
declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use  app\admin\traits\ValidateTrait;
use think\exception\ValidateException;

class Message extends Base
{
    use ValidateTrait; // 使用 Trait
    ##员工信息
    protected $request;
    protected $service;
    protected $operator_id;
    protected $validationScenes = [
        'index' => [
            'pageIndex' => ['require|integer', '页码'],
            'pageSize' => [['require', 'between:10,100'], '每页数量'],
            'title' => ['string', '标题'],

        ],
        'delete' => [
            'ids' => ['require', 'id'],
        ],
        'handle' => [
            'id' => ['require|integer', 'id'],
        ],
    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\Message();

        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe 站内信列表
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('index');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $pageIndex = isset($params['pageIndex']) ? intval($params['pageIndex']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 10;
        $title = isset($params['title']) ? $params['title'] : null;
        $createTime = isset($params['create_time']) ? trim($params['create_time']) : null;
        $status = isset($params['status']) ? intval($params['status']) : null;

        $where = ['is_del' => 0];
        if ($title) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        if ($status > -1) {
            $where['status'] = $status;
        }
        if ($createTime) {
            $where[] = ['create_time', 'between', [$createTime . ' 00:00:00', $createTime . ' 23:59:59']];
        }

        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 删除销售人员
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('delete');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $res = $this->service->delete($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 删除销售人员
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function handle()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('handle');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $res = $this->service->handle($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-11 17:02:30
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum(Request $request)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $this->service->getFieldEnum($param);
        return json($arrData);
    }

}